<template>
  <div class="prescription-print-view" v-if="patientData">
    <!-- 检查治疗项打印 -->
    <div class="print-main" v-if="checkItemList.length||surchargeList.length" style="padding: 20px; page-break-after: always">
      <prescription-header
        titles="诊疗费用单"
        :types="2"
        :patientData="patientData"
      ></prescription-header>
      <div class="prescription-container">
        <check-item-list
          :checkList="checkItemList"
          :surchargeList="surchargeList"
        ></check-item-list>
      </div>
      <prescription-footer :patientData="checkItemFooter"></prescription-footer>
    </div>
    <!-- 成药处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-for="(item, index) in chengyaoList"
      :key="'cycf' + index"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <cy-prescription-list :prescriptionItem="item"></cy-prescription-list>
      </div>
      <prescription-footer :patientData="item.footerData"></prescription-footer>
    </div>
    <!-- 饮片处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-for="(item, index) in yinpianList"
      :key="'ypcf' + index"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <yp-prescription-list :prescriptionItem="item"></yp-prescription-list>
      </div>
      <prescription-footer :patientData="item.footerData">
        <template slot="yipian-footer">
          <div class="yipian-footer">
            <div class="left">
              <span class="left-title">用量：</span>
              <p>
                <span>{{ item.remark[1].yf }}</span
                ><span>{{ item.remark[1].mrjl }}剂/日</span>
              </p>
            </div>
            <div class="right">
              <span class="left-title">用法：</span>
              <p>
                <span>{{ item.remark[1].yf }}</span>
                <span>{{ item.remark[1].fyyq }}</span>
              </p>
            </div>
          </div>
        </template>
      </prescription-footer>
    </div>
    <!-- 输液处方打印 -->
    <div
      class="print-main"
      style="padding: 20px; page-break-after: always"
      v-for="(item, index) in shuyeList"
      :key="'sycf' + index"
    >
      <prescription-header :patientData="patientData"></prescription-header>
      <div class="prescription-container">
        <sy-prescription-list :prescriptionItem="item"></sy-prescription-list>
      </div>
      <prescription-footer :patientData="item.footerData"></prescription-footer>
    </div>
  </div>
</template>
<script>
import prescriptionHeader from "./prescriptionHeader.vue";
import prescriptionFooter from "./prescriptionFooter.vue";
import checkItemList from "./checkItemList.vue";
import cyPrescriptionList from "./cyPrescriptionList.vue";
import ypPrescriptionList from "./ypPrescriptionList.vue";
import syPrescriptionList from "./syPrescriptionList.vue";
export default {
  components: {
    prescriptionHeader,
    prescriptionFooter,
    checkItemList,
    cyPrescriptionList,
    ypPrescriptionList,
    syPrescriptionList,
  },
  props: {
    patientData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      checkItemList: [],
      checkItemFooter: {},
      chengyaoList: [],
      yinpianList: [],
      shuyeList: [],
      surchargeList: [],
    };
  },
  watch: {
    patientData: {
      handler(newval, oldval) {
          this.chengyaoList = []
          this.yinpianList = []
          this.shuyeList = []
        if (newval) {
          if (!Array.isArray(newval.age)) {
            newval.age = newval.age.split(",").map((value) => {
              return Number(value);
            });
          }
          if (newval.check_items) {
            this.checkItemFooter = {
              doctor_name: newval.doctor_name,
              amount: 0,
            };
            this.checkItemList = [];
            newval.check_items.forEach((check, checkidx) => {
              let priceAmount =
                Number(check.check_item_number) * Number(check.price);
              check.amount = priceAmount;
              this.checkItemFooter.amount += priceAmount;
              this.checkItemList.push(check);
            });
          }
          this.surchargeList = []
          if (newval.surcharge) {
            let total = 0;
            newval.surcharge.forEach((itm, idx) => {
              total += itm.amount;
              this.surchargeList.push(itm)
            });
            this.checkItemFooter.amount += total;
          }
          if (newval && newval.prescriptions.length > 0) {

            newval.prescriptions.forEach((item, index) => {
              let footerData = {
                doctor_name: newval.doctor_name,
                amount: 0,
              };
              Object.assign(item, { amount: 0, footerData: footerData });
              if (item.details) {
                item.details.forEach((det, detidx) => {
                  item.amount += Number(det.price) * Number(det.number);
                });
              }
              item.footerData.amount = item.amount;

              if (item.category_key == "cycf") {
                this.chengyaoList.push(item);
              } else if (item.category_key == "ypcf") {
                this.yinpianList.push(item);
              } else {
                this.shuyeList.push(item);
              }
            });
          }
        }
      },
      immediate: true,
      deep: true
    },
  },
  mounted() {
    // if (
    //   this.patientData &&
    //   this.patientData.prescriptions.length > 0
    // ) {
    //   this.patientData.prescriptions.forEach((item, index) => {
    //     let footerData = {
    //       doctor_name: this.patientData.doctor_name,
    //       amount: 0
    //     }
    //     Object.assign(item,{amount: 0, footerData: footerData})
    //     if (item.details) {
    //       item.details.forEach((det, detidx) => {
    //         item.amount += Number(det.price) * Number(det.number);
    //       });
    //     }
    //     item.footerData.amount = item.amount
    //     if (item.category_key == "cycf") {
    //       this.chengyaoList.push(item);
    //     } else if (item.category_key == "ypcf") {
    //       this.yinpianList.push(item);
    //     } else {
    //       this.shuyeList.push(item);
    //     }
    //   });
    // }
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/print/prescriptionPrint.scss";
</style>
